package com.imooc.case_class

import scala.util.Random

object CaseClassApp extends App{

  val names = Array("Akiho Yoshizawa","YuiHatno","Aoi Sola")

  val name = names(Random.nextInt(names.length))

  name match {

    case "Akiho Yoshizawa" => println("吉老师")
    case "YuiHatno" => println("波老师")
    case _ => println("你在说什么")

  }

  def judgeGrade(grade:String):Unit = {

    grade match {

      case "A" => println("Excellent ... ")
      case "B" => println("Good ... ")
      case "C" => println("Not Bad ... ")
      case _ => println("You Need Work Hard ... ")

    }

  }

  judgeGrade("A")
  judgeGrade("C")
  judgeGrade("D")


  def judgeGrade2(name:String , grade:String):Unit = {

    grade match {

      case "A" => println("Excellent ... ")
      case "B" => println("Good ... ")
      case "C" => println("Not Bad ... ")
      case _ if(name == "lisi") => println(name + " Just So So... ")
      case _ => println(name + "You Need Work Hard ... ")

    }

  }

  judgeGrade2("zs","D")
  // 优先走 case 如果 case 匹配上了 则直接 使用 case 分支
  judgeGrade2("lisi","A")



  def greeting(array: Array[String]): Unit = {

    array match {

      case Array("zhangsan") => println("Hi:zhangsan")
      case Array(x,y) => println("Hi:" + x + " , " + y)
      case Array("zhangsan",_*) => println("Hi: zs and other friends ... ")
      case _ => println("Hi: everybody")

    }

  }

  greeting(Array("zhangsan"))
  greeting(Array("zhangsan","lisi"))
  greeting(Array("zhangsan","lisi","ww"))
  greeting(Array("ww"))


  def greeting(list:List[String]): Unit = {

    list match {

      case "zhangsan"::Nil => println("Hi:zhangsan")
      case x::y::Nil => println("Hi: " + x + " , " + y)
      case "zhangsan"::tail => println("Hi: zhangsan and other friends ... ")
      case _ => println("Hi: everybody")

    }

  }

  greeting(List("zhangsan","lisi","ww"))
  greeting(List("pk"))




  def matchType(obj:Any) :Unit ={

    obj match {

      case x:Int => println("Int")
      case s:String => println("String")
      case m:Map[_,_] => m.foreach(println)
      case _ => println("other type")

    }

  }

  matchType(1)
  matchType("1")
  matchType(1L)
  matchType(Map("k" -> "v"))


  def caseClassMatch(person: Person): Unit ={
    person match {

      case CTO(name,floor) => println("CTO")
      case Employee(name,floor) => println("EMP")
      case _ => println("OTHER")

    }
  }


  class Person
  case class CTO(name:String,floor:String) extends Person
  case class Employee(name:String,floor:String) extends Person
  case class Other(name:String) extends Person

  caseClassMatch(CTO("CTO","2"))
  caseClassMatch(Other("other"))


  val score = Map("PK"->"A","ZS"->"C")

  def getGrade(name:String): Unit ={

    val grade = score.get(name)

    grade match {
      case Some(grade) => println("some grade")
      case None => println("none")
    }
  }

  getGrade("PK")
  getGrade("ZS")
  getGrade("test")

}


